unit uHotspots;

interface

uses types;

type
//  TShowProc = Procedure(msg : string);

  THotSpot = object
    Box      : TRect;
    Name     : string; //returns this once item has been found
    Variance : integer; // Gives a buffer of this ammount on all sides.

    function InRange(L,T:integer):boolean;
  end;

  THotSpotList = object
    HSTotal  : integer;
    HotSpots : array of THotSpot;

    HSIndex : integer;
    HotSpot : tHotSpot;

    ObjectError : boolean;

    procedure Create;
    procedure Free;

    function HSLocation(L, T, W, H:integer):TRect;
    procedure Add(Location:tRect;nVariance:integer;ObjectName:string);
    function Next:boolean;
    function Search(L,T:integer):string;
  end;

var
  HotSpotList : THotSpotList;

implementation

///==============

function THotSpot.InRange(L,T:integer):boolean;
var VarianceBox:TRect;
begin
  if (Variance > 0) then
  begin
    VarianceBox.Left   := Box.Left   - Variance;
    VarianceBox.Top    := Box.Top    - Variance;
    VarianceBox.Right  := Box.Right  + Variance;
    VarianceBox.Bottom := Box.Bottom + Variance;
  end;

  Result := FALSE;

  if (L >= VarianceBox.Left) then
    if (T >= VarianceBox.Top) then
      if (L <= VarianceBox.Right) then
        if (T <= VarianceBox.Bottom) then
          Result := TRUE;
end;

///==============

procedure THotSpotList.Create;
begin
  HSIndex := -1;
  HSTotal := 0;
  SetLength( HotSpots, HSTotal );
  HotSpot.Box := HSLocation(-1,-1,-1,-1); // Setting to minus should mean the hotspot is outside the boundry of the screen.

  ObjectError := FALSE;
end;

procedure THotSpotList.Free;
begin
  HSIndex := -1;
  HSTotal := 0;
  SetLength( HotSpots, HSTotal );
  HotSpot.Box := HSLocation(-1,-1,-1,-1); // Setting to minus should mean the hotspot is outside the boundry of the screen.
end;

function THotSpotList.HSLocation(L, T, W, H:integer):TRect;
var Rect : TRect;
begin
//  Rect := TRect.Create;

  Rect.Left   := L;
  Rect.Top    := T;
  Rect.Right  := L+W;
  Rect.Bottom := T+H;

  Result := Rect;
end;

procedure THotSpotList.Add(Location:tRect;nVariance:integer;ObjectName:string);
begin
  Inc( HSTotal );
  SetLength( HotSpots, HSTotal );

  with HotSpots[HSTotal-1] do
  begin
    Box      := Location;
    Variance := nVariance;
    Name     := ObjectName;
  end;
end;

function THotSpotList.Next:boolean;
begin
  Inc( HSIndex );

  Result := TRUE;

  if (HSIndex <= HSTotal)and(HSTotal > 0) then
    HotSpot := HotSpots[HSIndex]
  else
    Result := FALSE;
end;

function THotSpotList.Search(L,T:integer):string;
begin
  Result := '';

  if (HSIndex >= HSTotal)or(HSTotal <= 0) then
    ObjectError := TRUE
  else
  begin
    while (HotSpotList.Next) do
    begin
      if (HotSpot.InRange(L,T)) then
        Result := HotSpot.Name;
    end;
  end;
end;

end.
